package model

import (
	"errors"
	"haodayizhi/global"
	"time"

	"gorm.io/gorm"
)

const TableNameNotificationRecipient = "notification_recipients"

// NotificationRecipient mapped from table <notification_recipients>
type NotificationRecipient struct {
	ID             int64     `gorm:"column:id;type:bigint;primaryKey;autoIncrement:true" json:"id,string"`
	NotificationID int64     `gorm:"column:notification_id;type:bigint" json:"notificationId"`
	RecipientUID   int64     `gorm:"column:recipient_uid;type:bigint" json:"recipientUid"`
	Status         string    `gorm:"type:enum('unread', 'read');default:'unread'"`
	ReadTime       time.Time `gorm:"column:read_time;default:'NULL'" json:"read_time"`
}

// TableName NotificationRecipient's table name
func (*NotificationRecipient) TableName() string {
	return TableNameNotificationRecipient
}

// CreateNotificationRecipient creates a new NotificationRecipient in the database.
func (m *NotificationRecipient) CreateNotificationRecipient() error {
	if err := global.Db.Create(m).Error; err != nil {
		return err
	}
	return nil
}

// GetNotificationRecipientByID retrieves a NotificationRecipient by its ID from the database.
func (m *NotificationRecipient) GetNotificationRecipientByID(id int64) (*NotificationRecipient, error) {
	result := global.Db.First(m, id)
	if result.Error != nil {
		if errors.Is(result.Error, gorm.ErrRecordNotFound) {
			return nil, nil // NotificationRecipient not found
		}
		return nil, result.Error
	}
	return m, nil
}

// UpdateNotificationRecipient updates an existing NotificationRecipient in the database.
func (m *NotificationRecipient) UpdateNotificationRecipient() error {
	if err := global.Db.Save(m).Error; err != nil {
		return err
	}
	return nil
}

// DeleteNotificationRecipient deletes a NotificationRecipient by its ID from the database.
func (m *NotificationRecipient) DeleteNotificationRecipient(id int64) error {
	if err := global.Db.Delete(m, id).Error; err != nil {
		return err
	}
	return nil
}

// UpdateNotificationRecipientStatus 更新通知接收者的状态
func UpdateNotificationRecipientStatus(notificationID, recipientUID int64, status string) error {
	// 更新通知接收者的状态
	if err := global.Db.Model(&NotificationRecipient{}).
		Where("notification_id = ? AND recipient_uid = ?", notificationID, recipientUID).
		Update("status", status).
		Update("read_time", time.Now()).Error; err != nil {
		return err
	}
	return nil
}

// 获取用户接收到了那些通知以及通知状态
func (m *NotificationRecipient) GetNotificationRecipientByUID(uid int64, pageNum int, pageSize int) ([]*NotificationRecipient, int64, error) {
	var notifications []*NotificationRecipient
	var total int64
	result := global.Db.Where("recipient_uid = ?", uid).Order("id desc").Offset((pageNum - 1) * pageSize).Limit(pageSize).Find(&notifications)
	if result.Error != nil {
		return nil, total, result.Error
	}
	// 获取总数
	global.Db.Model(&NotificationRecipient{}).Where("recipient_uid = ?", uid).Count(&total)
	return notifications, total, nil
}

// 根据 多个通知id 创建通知接收者
func CreateAllNotificationRecipient(notificationIDs []int64, noticeID int64) error {
	var notificationRecipients []*NotificationRecipient
	for _, v := range notificationIDs {
		notificationRecipients = append(notificationRecipients, &NotificationRecipient{
			NotificationID: noticeID,
			RecipientUID:   v,
		})
	}
	if err := global.Db.Create(&notificationRecipients).Error; err != nil {
		return err
	}
	return nil
}
